package cn.quickcode.monitor.ui.dialog;

import java.sql.SQLException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;

import cn.quickcode.monitor.db.DbHelper;
import cn.quickcode.monitor.model.Dbm;
import cn.quickcode.monitor.ui.part.DBMViewPart;

public class NewDBMDialog extends Dialog
{
	public ViewPart viewPart;
	public Label lblNameLabel;
	public Label lblIpAddressLabel;
	public Label lblDriverLabel;
	public Label lblPortLabel;
	public Text txtName;
	public Text txtIpAddress;
	public Text txtPort;
	public Label lblUserNameLabel;
	public Label lblPasswordLabel;
	public Text txtUserName;
	public Text txtPassword;
	public Combo cbDriver;
	
	public Label lblAliasLabel;
	public Text txtAlias;
	public Label lblMaxAcitve;
	public Label lblMaxTableSpaceUse;
	public Text txtMaxActive;
	public Text txtMaxTableSpaceUse;
	
	public static final int BTN_OK_ID=1;
	public static final int BTN_CANCEL_ID=0;
	public static final int BTN_TEST_ID=2;
	public static final String BTN_OK_LABEL="OK";
	public static final String BTN_CANCEL_LABEL="Cancel";
	public static final String BTN_TEST_LABLE="Test";
	/**
	 * @wbp.parser.constructor
	 */
	public NewDBMDialog(Shell parentShell,ViewPart viewPart)
	{
		super(parentShell);
		this.viewPart = viewPart;
	}
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	private NewDBMDialog(Shell parentShell)
	{
		super(parentShell);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell newShell)
	{
		// TODO Auto-generated method stub
		super.configureShell(newShell);
		newShell.setText("添加监控");
		newShell.setImage(ResourceManager.getPluginImage("SuperMonitor", "icons/icon0.gif"));
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent)
	{
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(4, false);
		gl_container.marginRight = 10;
		gl_container.marginLeft = 10;
		gl_container.marginTop = 10;
		container.setLayout(gl_container);
		
		this.lblNameLabel = new Label(container, SWT.NONE);
		this.lblNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblNameLabel.setText("库称：");
		
		this.txtName = new Text(container, SWT.BORDER);
		this.txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblAliasLabel = new Label(container, SWT.NONE);
		this.lblAliasLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblAliasLabel.setText("别名：");
		
		this.txtAlias = new Text(container, SWT.BORDER);
		this.txtAlias.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblUserNameLabel = new Label(container, SWT.NONE);
		this.lblUserNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblUserNameLabel.setText("用户：");
		
		this.txtUserName = new Text(container, SWT.BORDER);
		this.txtUserName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblPasswordLabel = new Label(container, SWT.NONE);
		this.lblPasswordLabel.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1));
		this.lblPasswordLabel.setText("密码：");
		
		this.txtPassword = new Text(container, SWT.BORDER);
		this.txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblIpAddressLabel = new Label(container, SWT.NONE);
		this.lblIpAddressLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblIpAddressLabel.setText("地址：");
		
		this.txtIpAddress = new Text(container, SWT.BORDER);
		this.txtIpAddress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblPortLabel = new Label(container, SWT.NONE);
		this.lblPortLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblPortLabel.setText("端口：");
		
		this.txtPort = new Text(container, SWT.BORDER);
		this.txtPort.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblDriverLabel = new Label(container, SWT.NONE);
		this.lblDriverLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblDriverLabel.setText("驱动：");
		
		this.cbDriver = new Combo(container, SWT.READ_ONLY);
		this.cbDriver.setItems(new String[] {"oracle.jdbc.driver.OracleDriver"});
		this.cbDriver.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		this.lblMaxAcitve = new Label(container, SWT.NONE);
		this.lblMaxAcitve.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblMaxAcitve.setText("并发数：");
		
		this.txtMaxActive = new Text(container, SWT.BORDER);
		this.txtMaxActive.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblMaxTableSpaceUse = new Label(container, SWT.NONE);
		this.lblMaxTableSpaceUse.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblMaxTableSpaceUse.setText("表空间：");
		
		this.txtMaxTableSpaceUse = new Text(container, SWT.BORDER);
		this.txtMaxTableSpaceUse.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		return container;
	}
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent)
	{
		createButton(parent, BTN_TEST_ID, BTN_TEST_LABLE, true);
		createButton(parent, BTN_OK_ID, BTN_OK_LABEL, true);
		createButton(parent, BTN_CANCEL_ID, BTN_CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize()
	{
		return new Point(450, 248);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 */
	@Override
	protected void buttonPressed(int buttonId)
	{
		if(BTN_OK_ID == buttonId)
		{
			Dbm dbm = new Dbm();
			dbm.setName(this.txtName.getText());
			dbm.setAlias(this.txtAlias.getText());
			dbm.setUsername(this.txtUserName.getText());
			dbm.setPassword(this.txtPassword.getText());
			dbm.setIp(this.txtIpAddress.getText());
			dbm.setPort(this.txtPort.getText());
			dbm.setDriver(this.cbDriver.getText());
			dbm.setMaxActive(Integer.parseInt(this.txtMaxActive.getText()));
			dbm.setMaxTableSpaceUse(Integer.parseInt(this.txtMaxTableSpaceUse.getText()));
			if(Dbm.save(dbm))
			{
				((DBMViewPart)this.viewPart).refresh(null);
				this.close();
			}
			else
			{
				MessageDialog.openInformation(Display.getCurrent().getActiveShell(),"提示","操作失败，请重试！");
			}
		}
		else if(BTN_CANCEL_ID == buttonId)
		{
			this.close();
		}
		else if(BTN_TEST_ID == buttonId)
		{
			doTestConnection();
		}
	}
	private void doTestConnection()
	{
		try
		{
			if(DbHelper.testConnection(this.txtIpAddress.getText(),this.txtPort.getText(),this.txtName.getText(),this.txtUserName.getText(),this.txtPassword.getText(),this.cbDriver.getText()))
			{
				MessageDialog.openInformation(Display.getCurrent().getActiveShell(),"提示","连接测试成功！");
			}
			else
			{
				MessageDialog.openInformation(Display.getCurrent().getActiveShell(),"提示","连接测试失败，请确认驱动、用户名、密码...");
			}
		}
		catch (SQLException e)
		{
			MessageDialog.openInformation(Display.getCurrent().getActiveShell(),"提示","测试过程出现异常，测试失败！");
		}
	}
}
